package com.ewhale.feitengguest.ui.auth;

import android.Manifest;
import android.os.Bundle;
import android.os.Handler;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.ewhale.feitengguest.R;
import com.ewhale.feitengguest.ui.user.MainActivity;
import com.ewhale.feitengguest.ui.shop.ShopMainActivity;
import com.ewhale.feitengguest.utils.Contants;
import com.orhanobut.hawk.Hawk;
import com.simga.library.activity.MBaseActivity;
import com.simga.library.base.IPresenter;
import com.simga.library.base.IView;
import com.simga.library.utils.HawkKey;
import com.simga.library.utils.LogUtil;
import com.simga.library.utils.SystemUtil;

import java.util.List;

import butterknife.BindView;
import pub.devrel.easypermissions.AfterPermissionGranted;
import pub.devrel.easypermissions.AppSettingsDialog;
import pub.devrel.easypermissions.EasyPermissions;

public class SplashActivity extends MBaseActivity<IPresenter> implements IView, EasyPermissions.PermissionCallbacks {


    @BindView(R.id.tvVersion)
    TextView mTvVersion;

    private static final int REQUEST_CODE_PERMISSION_2 = 10000;

    @Override
    protected void onGetBundle(Bundle bundle) {
    }

    @Override
    protected int getViewId() {
        return R.layout.activity_splash;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        mTvVersion.setText("当前版本: " + SystemUtil.getVersionName(mContext));
        requestPermissionsMain();
    }


    @AfterPermissionGranted(REQUEST_CODE_PERMISSION_2)
    private void requestPermissionsMain() {
        String[] perms = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE};
        if (EasyPermissions.hasPermissions(mContext, perms)) {
            startHandler();
        } else {
            EasyPermissions.requestPermissions(this, "申请获取相关权限",
                    REQUEST_CODE_PERMISSION_2, perms);
        }
    }

    @Override
    public void onPermissionsGranted(int requestCode, @NonNull List<String> perms) {
    }

    @Override
    public void onPermissionsDenied(int requestCode, @NonNull List<String> perms) {
        if (EasyPermissions.somePermissionPermanentlyDenied(this, perms)) {
            new AppSettingsDialog.Builder(this).setTitle("权限申请")
                    .setRationale("申请获取相关权限")
                    .setPositiveButton(getString(R.string.comfir))
                    .setNegativeButton(getString(R.string.cancle))
                    .build()
                    .show();
        }else{
            startHandler();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }


    private void startHandler() {
        boolean firstIn = Hawk.get(HawkKey.FIRST_IN, true);
        if (firstIn) {
            startActivity(null, GuideActivity.class);
            finish();
        } else {
            new Handler().postDelayed(() -> {
                if(Hawk.get(HawkKey.NOW_APP_STATUS,Contants.USER_STATUS).equals(Contants.SHOP_STATUS)){
                    ShopMainActivity.open(mContext);
                }else{
                    MainActivity.open(mContext);
                }
                finish();
            }, 3000);
        }
    }

    @Override
    protected void initListener() {
    }

    @Override
    protected boolean isUseBaseTitleBar() {
        return false;
    }

    @Override
    public void showProLoading() {
        showLoading();
    }

    @Override
    public void dimissProLoading() {
        dismissLoading();
    }

    @Override
    public void showErrorMessage(int postCode, String code, String message) {
        showErrorMsg(code, message);
    }

}
